package com.tbit.uqbike.service.business.impl;

import cn.hutool.core.util.StrUtil;
import com.tbit.common.config.OssDefaultConfig;
import com.tbit.common.entity.view.Result;
import com.tbit.uqbike.object.pojo.OssConfig;
import com.tbit.uqbike.service.business.ProtocolService;
import com.tbit.uqbike.webmanager.dao.core.OssConfigDao;
import com.tbit.uqbike.webmanager.util.aliyun.OssFileUtils;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.Optional;

/**
 * 协议自定义
 *
 * @author <a href="mailto:liu.haoxiang@tbit.com.cn">BUGXiang</a>
 * @version 1.0
 * @since 2025-4-7
 */
@Service
public class ProtocolServiceImpl implements ProtocolService {

    @Value("${oss.useBuketName:gx-mp}")
    private String useBuketName;

    @Value("${oss.usePath:userAgreement-new}")
    private String usePath;

    @Value("${oss.privacyPath:privacy}")
    private String privacyPath;



    @Resource
    private OssDefaultConfig ossDefaultConfig;

    @Resource
    private OssConfigDao ossConfigDao;


    /**
     * 获取协议内容
     *
     * @param brandId 品牌id
     * @param type    类型 0-使用协议 1-隐私协议
     * @return Result
     */
    @Override
    public Result<Object> getProtocol(Integer brandId, Integer type) {
        OssConfig ossConfig = new OssConfig(ossDefaultConfig.getEndpoint(),
                        ossDefaultConfig.getAccessKeyId(),
                        ossDefaultConfig.getAccessKeySecret(), useBuketName);
        return Result.success(OssFileUtils.readFile(ossConfig.getBucketName(), brandId + ".html", ossConfig, type == 0 ? usePath : privacyPath));
    }


    /**
     * 编辑协议
     *
     * @param brandId 品牌id
     * @param type    类型 0-使用协议 1-隐私协议
     * @param content 内容
     * @return
     */
    @Override
    public Result<Object> editProtocol(Integer brandId, Integer type, String content) {
        if (StrUtil.isBlank(content)) {
            return Result.error("协议内容不能为空");
        }
        OssConfig ossConfig = new OssConfig(ossDefaultConfig.getEndpoint(),
                        ossDefaultConfig.getAccessKeyId(),
                        ossDefaultConfig.getAccessKeySecret(), useBuketName);
        /*构建html文件名称*/
        String fileName = brandId + ".html";
        OssFileUtils.createFileUpload(ossConfig, ossConfig.getBucketName(), fileName,  type == 0 ? usePath : privacyPath, content);
        return Result.success();
    }


}
